#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>

struct query { int x1, y1; int x2, y2; int k; };

struct data { int x, y; int v; };

bool operator<(data a, data b)
{
	return a.v < b.v || (a.v == b.v && (a.x < b.x || (a.x == b.x && a.y < b.y)));
}

int const maxn = 402;
int const maxq = 50002;

std::vector<data> d;
std::vector<query> q;

int da[maxn][maxn];
int sum[maxn][maxn];
int tot[maxn][maxn];
bool vis[maxq];
int ans[maxq];
int n, m, q_num;

int calc_num(int x1, int y1, int x2, int y2)
{
	return tot[x2][y2] - tot[x2][y1-1] - tot[x1-1][y2] + tot[x1-1][y1-1];
}

int inrange(int x, int y, int i)
{
	return (q[i].x1 <= x && x <= q[i].x2) && (q[i].y1 <= y && y <= q[i].y2);
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T;
	std::cin >> T;
	for (int ti = 1; ti <= T; ti++) {
		std::cout << "Case #" << ti << ":\n";
		std::memset(da, 0, sizeof(da));
		std::memset(vis, 0, sizeof(vis));
		d.clear();

		std::cin >> n >> m >> q_num;
		for (int i = 1; i <= n; i++)
			for (int j = 1, t; j <= m; j++) {
				std::cin >> t;
				data tmp;
				tmp.x = i; tmp.y = j; tmp.v = t;
				d.push_back(tmp);
			}

		q.clear();
		q.resize(q_num);
		for (int i = 0; i < q_num; i++)
			std::cin >> q[i].x1 >> q[i].y1 >> q[i].x2 >> q[i].y2 >> q[i].k;

		std::sort(d.begin(), d.end());
		int square = n * m;
		int size = std::sqrt(square * square/q_num + square);
		int all = 0;
		for (; all < n*m; all++) {
			da[d[all].x][d[all].y] = d[all].v;
			if (!((all+1) % size)) {
				for (int i = 1; i <= n; i++)
					for (int j = 1; j <= m; j++) {
						tot[i][j] = tot[i][j-1] + tot[i-1][j] - tot[i-1][j-1];
						if (da[i][j]) tot[i][j]++;
					}

				for (int i = 0; i < q_num; i++) {
					if (vis[i]) continue;
					int num = calc_num(q[i].x1, q[i].y1, q[i].x2, q[i].y2);
					if (num >= q[i].k) {
						vis[i] = true;
						int tot_last = 0;
						for (int j = all - size + 1; j <= all; j++)
							if (inrange(d[j].x, d[j].y, i)) tot_last++;

						int index = q[i].k - (num - tot_last), kth = 0;
						for (int j = all - size + 1; j <= all; j++) {
							if (inrange(d[j].x, d[j].y, i)) {
								kth++;
								if (kth == index) {
									ans[i] = d[j].v;
									break;
								}
							}
						}
					}
				}
			}
		}

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				tot[i][j] = tot[i][j-1] + tot[i-1][j] - tot[i-1][j-1];
				if (da[i][j]) tot[i][j]++;
			}

		/*
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				std::cout << tot[i][j] << ' ';
			}
			std::cout << '\n';
		}
		std::cout << " ---->" << q[1].x1 << " " <<  q[1].y1 << " " <<   q[1].x2 << " " <<  q[1].y2 << '\n';
		std::cout << " ---->" << calc_num(q[1].x1, q[1].y1, q[1].x2, q[1].y2) << '\n';
*/

	//	std::cout << "size: " << size << '\n';

		all = n*m;
		for (int i = 0; i < q_num; i++) {
			if (vis[i]) continue;
			int num = calc_num(q[i].x1, q[i].y1, q[i].x2, q[i].y2);
			if (num >= q[i].k) {
				//std::cout << "query: " << i+1 << '\n';
				//
				vis[i] = true;
				int tot_last = 0;
				for (int j = int(all/size) * size; j < all; j++)
					if (inrange(d[j].x, d[j].y, i)) tot_last++;

				//std::cout << "tot_last: " << tot_last << '\n';

				int index = q[i].k - (num - tot_last), kth = 0;
				for (int j = int(all/size) * size; j < all; j++) {
					if (inrange(d[j].x, d[j].y, i)) {
						kth++;
						if (kth == index) {
							ans[i] = d[j].v;
							break;
						}
					}
				}
			}
		}
		for (int i = 0; i < q_num; i++)
			std::cout << ans[i] << '\n';
	}
}
